package vehiclebase.utils;


import java.io.*;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * <li>Title: ToolsUtils.java</li> <li>Project: account</li> <li>Package: com.sinosoft.account.util</li> <li>
 * Description:</li> <li>Copyright: Copyright (c) 2008</li> <li>Company: sinosoft</li> <li>Created on Mar 13, 2009
 * 8:15:37 PM</li>
 *
 * @author lisitao
 * @version 1.0
 */

public class ToolsUtils {
    public static final String NONE = "none";
    public static final String SUCCESS = "success";
    public static final String FAILED = "failed";
    public static final String RESULTTYPE = "resultType";
    public static final String RESULTVALUE = "resultValue";
    public static final String MESSAGE = "message";
    public static final String PATTERN_NUMBER="[0-9]*";

    public static final int VERIFICATION_CODE_LENGTH = 6;//验证码默认长度

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getDate() {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        return date.format(today);
    }

    /**
     * 获取当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getDateToSecondStr() {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date today = new Date();
        return date.format(today);
    }

    /**
     * 获得当月第一天
     *
     * @return
     */
    public static String getFirstDayOfMonth() {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        return date.format(today).substring(0, 8) + "01";
    }


    /**
     * 获得给定年月的最后一天
     *
     * @param startDate
     * @return
     */
    public static String getLastDayOfMonth(String startDate) {
        String[] t = startDate.split("-");
        String year = t[0];
        String month = t[1];
        Calendar cal = Calendar.getInstance();
        // 年
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        // 月，因为Calendar里的月是从0开始，所以要-1
        cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);
        // 月份加一，得到下个月的一号
        cal.add(Calendar.MONTH, 1);
        // 下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);
        return t[0] + "-" + t[1] + "-" + String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号
    }

    /**
     * 判断对象为空
     */
    public static boolean isEmpty(final Object obj) {
        return (obj == null);
    }

    /**
     * 判断对象不为空
     *
     * @param obj
     * @return true-不为空 false-为空
     */
    public static boolean notEmpty(final Object obj) {
        return !isEmpty(obj);
    }

    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str.trim())) {
            return true;
        }
        return false;
    }

    public static boolean isEmpty(final Collection<?> obj) {
        return (obj == null) || obj.isEmpty();
    }

    public static boolean isEmpty(String[] items) {
        if (items == null || items.length < 1) {
            return true;
        }
        return false;
    }

    public static boolean notEmpty(String str) {
        if (str != null && !"".equals(str.trim())) {
            return true;
        }
        return false;
    }

    public static boolean notEmpty(final Collection<?> obj) {
        return !isEmpty(obj);
    }

    public static boolean notEmpty(String[] items) {
        if (items != null && items.length > 0) {
            return true;
        }
        return false;
    }

    /**
     * 取得默认的日期格式
     *
     * @return
     */
    public static String getDefaultDateFormat() {
        // 用方法的形式提供默认的日期格式，便于维护日期格式的取得来源和格式内容。
        return "yyyy-MM-dd";
    }

    /**
     * 转换Date为指定格式的字符串
     *
     * @return String
     */
    public static String convertDateToString(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }


    /**
     * 本方法用于将yyyy-MM-dd型的字符串转换成日期类型，如果strDate为Null或者为空字符串,则返回null
     *
     * @param strDate
     * @return
     * @throws Exception
     *             Date Author Skify Jul 9, 2011
     */
    public static Date getDateObject(String strDate) throws Exception {
        Date dt_Date1 = null;
        if (strDate == null || strDate.trim().length() == 0) {
            return null;
        }
        SimpleDateFormat simdf = new SimpleDateFormat(getDefaultDateFormat());
        try {
            dt_Date1 = simdf.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return dt_Date1;
    }

    /**
     * 转换字符串成按指定格式日期
     *
     * @param strDate
     * @param format
     * @return
     * @throws Exception
     *             Date Author: xinyu.li 2012-11-6
     */
    public static Date getDateObjectForMin(String strDate, String format) throws Exception {
        Date dt_Date1 = null;
        if (strDate == null || strDate.trim().length() == 0) {
            return null;
        }
        SimpleDateFormat simdf = new SimpleDateFormat(format);
        try {
            dt_Date1 = simdf.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return dt_Date1;
    }

    /**
     * 检测是否数字
     *
     * @param string
     * @return boolean Author Skify Aug 21, 2011
     */
    public static boolean checkIsInteger(String string) {
        if (isEmpty(string)) {
            return false;
        }
        char[] c = string.toCharArray(); // 把输入的字符串转成字符数组
        for (int i = 0; i < c.length; i++) {
            if (!Character.isDigit(c[i])) { // 判断是否为数字
                return false;
            }
        }
        return true;
    }

    /**
     * 检测是否yyyy-MM-dd日期类型
     *
     * @param string
     * @return boolean Author Skify Aug 21, 2011
     */
    public static boolean checkIsDate(String string) {
        if (isEmpty(string)) {
            return false;
        }
        string = string.trim();
        if (string.length() != 10) {
            return false;
        }
        // 年
        String year = string.substring(0, 4);
        char[] years = string.substring(0, 4).toCharArray();
        for (int i = 0; i < years.length; i++) {
            if (!Character.isDigit(years[i])) { // 判断是否为数字
                return false;
            }
        }
        Integer yearIn = Integer.valueOf(year);
        // 符号
        if (!"-".equals(string.substring(4, 5)) || !"-".equals(string.substring(7, 8))) {
            return false;
        }
        // 月
        String month = string.substring(5, 7);
        char[] months = month.toCharArray();
        for (int i = 0; i < months.length; i++) {
            if (!Character.isDigit(months[i])) { // 判断是否为数字
                return false;
            }
        }
        Integer monthIn = Integer.valueOf(month);
        if (monthIn > 12) {
            return false;
        }
        // 日
        String day = string.substring(8, 10);
        char[] days = day.toCharArray();
        for (int i = 0; i < days.length; i++) {
            if (!Character.isDigit(days[i])) { // 判断是否为数字
                return false;
            }
        }
        Integer dayIn = Integer.valueOf(day);
        if (dayIn > 31) {
            return false;
        }
        // 月日
        if ("02".equals(month)) {
            if ("00".equals(year.substring(2, 4))) {
                if (yearIn % 400 == 0) {
                    if (dayIn > 29) {
                        return false;
                    }
                } else {
                    if (dayIn > 28) {
                        return false;
                    }
                }
            } else {
                if (yearIn % 4 == 0) {
                    if (dayIn > 29) {
                        return false;
                    }
                } else {
                    if (dayIn > 28) {
                        return false;
                    }
                }
            }
        } else if ("01".equals(month) || "03".equals(month) || "05".equals(month) || "07".equals(month)
                || "08".equals(month) || "10".equals(month) || "12".equals(month)) {
            if (dayIn > 31) {
                return false;
            }
        } else {
            if (dayIn > 30) {
                return false;
            }
        }
        return true;
    }

    /**
     * return Integer 获取两个日期间的天数
     *
     * @param dateBegin
     * @param dateEnd
     * @return Integer Author: Skify Aug 16, 2012
     */
    public static Integer getDaysOfTwoDateTime(Date dateBegin, Date dateEnd) {
        Long quot = dateEnd.getTime() - dateBegin.getTime();
        quot = quot / 1000 / 60 / 60 / 24;
        return Integer.valueOf(String.valueOf(quot));
    }

    /**
     * BigDecimal计算：加
     *
     * @param one
     * @param two
     * @return author --- Skify -- 2013-9-6下午02:41:53---------------- return Double
     */
    public static Double add(Double one, Double two) {
        if (one == null) {
            one = 0d;
        }
        if (two == null) {
            two = 0d;
        }
        BigDecimal calResult = new BigDecimal(one).add(new BigDecimal(two));
        return calResult.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * BigDecimal计算：减
     *
     * @param one
     * @param two
     * @return author --- Skify -- 2013-9-6下午02:41:53---------------- return Double
     */
    public static Double subtract(Double one, Double two) {
        if (one == null) {
            one = 0d;
        }
        if (two == null) {
            two = 0d;
        }
        BigDecimal calResult = new BigDecimal(one).subtract(new BigDecimal(two));
        return calResult.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * BigDecimal计算：乘
     *
     * @param one
     * @param two
     * @return author --- Skify -- 2013-9-6下午02:41:53---------------- return Double
     */
    public static Double multiply(Double one, Double two) {
        if (one == null) {
            one = 0d;
        }
        if (two == null) {
            two = 0d;
        }
        BigDecimal calResult = new BigDecimal(one).multiply(new BigDecimal(two));
        return calResult.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * BigDecimal计算：除
     *
     * @param one
     * @param two
     * @return author --- Skify -- 2013-9-6下午02:41:53---------------- return Double
     */
    public static Double divide(Double one, Double two) {
        if (one == null) {
            one = 0d;
        }
        if (two == null || two.equals(0d)) {
            return 0d;
        }
        BigDecimal calResult = new BigDecimal(one).divide(new BigDecimal(two), 10, BigDecimal.ROUND_HALF_UP);
        return calResult.setScale(10, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /**
     * String字符串转换成List<String>
     *
     * @param strs
     * @return
     */
    public static List<String> String2List(String strs) {
        String str[] = strs.split(",");
        return Arrays.asList(str);
    }

    /**
     * return Integer 获取两个日期间的秒数
     *
     * @param dateBegin
     * @param dateEnd
     * @return Integer
     */
    public static int getDaysOfTwoDateSecond(Date dateBegin,Date dateEnd) {
        long quot = dateEnd.getTime() - dateBegin.getTime();
        quot = quot / 1000;
        return (int)quot;
    }

    /**
     *  判断字符串是否为数字
     * @param str
     * @return 是数字返回true，否则返回false
     */
    private static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile(ToolsUtils.PATTERN_NUMBER);
        return pattern.matcher(str).matches();
    }

    /*
    *获取指定日期是星期几
    * String date = "2031-7-1";
     */
    public  String getWeek(String sdate) throws ParseException {
        // 再转换为时间
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Date parseDate = dateFormat.parse(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(parseDate);
        int week_index = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];


    }



}

